(* file operating on the interface *)

(* made using LablGTK *)
(* created by Sweepy *)
(* added to Subversion server *)


(* int > count the total of steps in the program for the progress bar *)
let numberTotalStep = 4
let iti_depart = ref (10,10)
let iti_arrivee = ref (10,10)

(******** variables ********)

(* map to parse and transform *)
let imageToParse = new MapPlan.mapPlan "empty"


(* int & string > return the current step *)
let currentStepInt = ref 0
let currentStepString = ref "Initialisation"


(* main window *)
let window =
	GMain.init ();
	let wnd = GWindow.window
		~title:"Let's map the world ! - Bêta version..."
		~resizable:true
		~width:1024
		~height:768 () in
		wnd#connect#destroy GMain.quit;
		wnd


(* vBox > verticalBox containing the whole interface *)
let boxGeneral = GPack.vbox ~packing:window#add ()


(* hBox > first horizontal box containing the toolbar *)
let hbox1 =
	let hbox = GPack.hbox
		~height: 60
		~packing:boxGeneral#add ()
	in hbox


(* hBox > second horizontal box containing the main part *)
let hbox2 =
	let hbox = GPack.hbox
		~height:550
		~packing:boxGeneral#add ()
	in hbox

(* hBox > third horizontal box containing the progress bar *)
let hbox3 =
	let hbox = GPack.hbox
		~height:15
		~packing:boxGeneral#add ()
	in hbox


(* progress_bar > display the processus state *)
let progressBar_bar =
	let pBar = GRange.progress_bar
		~pulse_step:0.5
		~packing:hbox3#add ()
	in
	pBar



(******* tab managing ********)
let tabControl = GPack.notebook
	~packing:hbox2#add ()


(******** home page asking the user to select an image ********)
let tabAccueil = GPack.vbox ()
let name_tabAccueil = GMisc.label ~text:"Initialisation" ()
let addTabAccueil = tabControl#insert_page
	~tab_label:name_tabAccueil#coerce tabAccueil#coerce



let contentImg23 = GBin.frame
	~packing:tabAccueil#add ()
let imgFrame = GMisc.image
	~file:"imgFiles/lmtw_little.jpg"
	~packing:contentImg23#add ()

(* GText.View > affiche à l'utilisateur les informations d'utilisation *)
(* en lisant les données dans le fichier accueil.txt *)
let textAccueil =
	let ic = open_in "textFiles/accueil.txt" in
	let n = in_channel_length ic in
	let s = String.create n in
	really_input ic s 0 n;
	close_in ic;
	let b = GText.buffer () in
		b#set_text (s);
		(*GText.view
			~editable:false
			~buffer:b
			~border_width:15
			~cursor_visible:false
			~width:400
			~wrap_mode:`WORD
			~packing:tabAccueil#add ()*)
		GMisc.label
			~text:s
			~width:800
			~line_wrap:true
			~packing:tabAccueil#add ()





(******** tab containing the echantillonnage parameters ********)
(* comprend une vue de l'image, et tous les paramètres de précision *)
let tab1 = GPack.vbox ()
let name_tab1 = GMisc.label ~text:"Paramétrage" ()
let addTab1 = tabControl#insert_page
	~tab_label:name_tab1#coerce tab1#coerce


let contentImgHBox =
	let box = GPack.vbox
		~packing:tab1#add ()
	in box

let contentParameters =
	let box = GPack.hbox
		~packing:tab1#add ()
	in box


(* Insertion de barres de défilement. *)
let scroll = GBin.scrolled_window
  ~height:80
  ~hpolicy:`ALWAYS
  ~vpolicy:`ALWAYS
  ~packing:contentImgHBox#add ()


let contentImg = GBin.frame
	~label:"Carte plane"
	~packing:scroll#add_with_viewport ()
let imgFrame = GMisc.image
	~packing:contentImg#add ()


let box1Left =
	let box = GPack.vbox
		~packing:contentParameters#add ()
	in box

let box1Middle =
	let box = GPack.vbox
		~homogeneous:false
		~border_width:40
		~packing:contentParameters#add ()
	in box

let box1Right =
	let box = GPack.vbox
		~packing:contentParameters#add ()
	in box


(* GMisc.label > affiche la précision de l'échantillonnage *)
let displayAccuracy =
	let label = GMisc.label
		~text:"Maillage d'échantillonnage : 500 px."
		~packing:box1Left#add ()
	in label


(* GRange.scale > permet de régler la précision *)
let fixAccuracy =
	let button = GRange.scale `VERTICAL
		~adjustment:(GData.adjustment ~lower:2. ~upper:110. ())
		~value_pos:`BOTTOM
		~digits:0
		~inverted:true
		~packing:box1Left#add ()
	in button


(* GMisc.label > affiche le niveau de correction des artefacts *)
let displayCorrectionArtefacts =
	let label = GMisc.label
		~text:"Correction des artefacts :\nnulle"
		~packing:box1Right#add ()
	in label


(* GRange.scale > permet de régler le niveau de correction des artefacts *)
let fixCorrection =
	let button = GRange.scale `VERTICAL
		~adjustment:(GData.adjustment ~lower:5. ~upper:110. ())
		~value_pos:`BOTTOM
		~digits:0
		~inverted:true
		~packing:box1Right#add ()
	in button


let heightMapFalse =
    let button = GButton.radio_button
	~label:"Echantillonnage couleur - sans lissage"
	~packing:box1Middle#add ()
    in button


let heightMapTrue =
    let button = GButton.radio_button
	~label:"Echantillonnage monochrome - avec lissage"
	~group:heightMapFalse#group
	~packing:box1Middle#add ()
    in button



(* separator_tool_item > separator between radio buttons *)
let separatorRadioButton1 =
	let separator = GMisc.separator `HORIZONTAL
		~packing:box1Middle#add ()
	in separator

(*check_button > indique si l'utilisateur veut utiliser les diagonales ou non *)
let wantTheObliques =
	let button = GButton.check_button
		~label:"Echantillonner les diagonales"
		~active:true
		~packing:box1Middle#add ()
	in button


(* check_button >
 * indique si l'utilisateur veut conserver les mêmes couleurs pour la 3D *)
let wantToSaveColors =
	let button = GButton.check_button
		~label:"Conserver les couleurs d'origine dans l'univers 3D généré"
		~active:true
		~packing:box1Middle#add ()
	in button


(* separator_tool_item > separator between radio buttons *)
let separatorRadioButton2 =
	let separator = GMisc.separator `HORIZONTAL
		~packing:box1Middle#add ()
	in separator


(* GMisc.label > demande à l'utilisateur le tracé qu'il souhaite obtenir *)
let whichTrace =
	let label = GMisc.label
		~text:"Quel type de tracé souhaitez-vous ?"
		~packing:box1Middle#add ()
	in label

(* GPack.hbox > contient les choix de tracé *)
let buttonTraces =
	let box = GPack.hbox
		~packing:box1Middle#add ()
	in box

(* GButton.radio_button > whichTrace - couleur noir *)
let buttonWhichTrace_noir =
	let button = GButton.radio_button
		~label:"noir"
		~packing:buttonTraces#add ()
	in button

(* GButton.radio_button > whichTrace - opacité moyenne *)
let buttonWhichTrace_opacityMedium =
	let button = GButton.radio_button
		~label:"opacité moyenne"
		~group:buttonWhichTrace_noir#group
		~packing:buttonTraces#add ()
	in button

(* GButton.radio_button > whichTrace - opacité faible *)
let buttonWhichTrace_transparent =
	let button = GButton.radio_button
		~label:"opacité faible"
		~group:buttonWhichTrace_noir#group
		~packing:buttonTraces#add ()
	in button


(******* tab containing the drafted map and the colors ********)
let tab2 = GPack.hbox ()
let name_tab2 = GMisc.label ~text:"Echantillonnage" ()
let addTab2 = tabControl#insert_page
	~tab_label:name_tab2#coerce tab2#coerce


let test () =
	name_tab2#set_text "coucouuuu"

(* contains the drafted map *)
let boxContentImg =
	let box = GPack.vbox
		~packing:tab2#add ()
	in box


(* GBin.scrolled_windows > insert the scrolling for the drafted map *)
let scroll_mapped = GBin.scrolled_window
  ~height:150
  ~hpolicy:`ALWAYS
  ~vpolicy:`ALWAYS
  ~packing:boxContentImg#add ()


(* GBin.scrolled_windows > insert the scrolling in the color list *)
let scroll_height = GBin.scrolled_window
  ~vpolicy:`ALWAYS
  ~packing:tab2#add ()


(* contains the heights and modelisation options *)
let boxContentOptions =
	let box = GPack.vbox
		~packing:scroll_height#add ()
	in box


(* GMisc.image > display drafted map *)
let contentImg_mapped = GBin.frame
	~label:"Carte pré-traitée"
	~packing:scroll_mapped#add_with_viewport ()
let imgFrame_mapped = GMisc.image
	~packing:contentImg_mapped#add ()



(******* tab containing the A* algo ******)
let tab25 = GPack.hbox ()
let name_tab25 = GMisc.label ~text:"Recherche d'itinéraire" ()
let addTab25 = tabControl#insert_page
	~tab_label:name_tab25#coerce tab25#coerce


let boxAStar =
	let box = GPack.vbox
		~width:300
		~packing:tab25#add ()
	in box

let boxChoosePoints =
	let box = GPack.vbox
		~width:500
		~packing:tab25#add ()
	in box




let scroll_algo = GBin.scrolled_window
  ~height:80
  ~hpolicy:`ALWAYS
  ~vpolicy:`ALWAYS
  ~packing:boxAStar#add ()


(* GMisc.image > display map and A* options *)
let contentImg_algo = GBin.frame
	~label:"Recherche d'itinéraire"
	~packing:scroll_algo#add_with_viewport ()


let eventBox_algo = GBin.event_box
	~packing:contentImg_algo#add ()


let imgFrame_algo = GMisc.image
	~packing:eventBox_algo#add ()


(* hbox > contains parameters and results *)
let box_parameters_algo =
	let box = GPack.vbox
		~packing:boxChoosePoints#add ()
	in box



let boxDisplayParameters =
    let box = GPack.hbox
		~width:500
		~packing:boxChoosePoints#add()
    in box


(* display instructions for choosing path *)
let instructions_label_algo =
	GMisc.label
		~text:"Cliquez sur la carte pour
		paramétrer vos points de départ et d'arrivée.
		Clic gauche > depart - Clic droit > arrivée"
		~packing:box_parameters_algo#add ()

let buttonTest = GButton.button
	~label:">> Calculer mon itinéraire"
	~packing:box_parameters_algo#add ()


let displayDenivelateBox =
	let box = GPack.vbox
		~packing:boxDisplayParameters#add ()
	in box

let displayTimingBox =
	let box = GPack.vbox
		~packing:boxDisplayParameters#add ()
	in box


let labelDenivelate =
    let label = GMisc.label
	~text:"Dénivelé montant : 100"
	~packing:displayDenivelateBox#add ()
    in label


let labelTiming =
    let label = GMisc.label
	~text:"Temps moyen estimé : 2h\nmarch à 2km/h"
	~packing:boxDisplayParameters#add ()
    in label


(******* tab containing the 3D navigation ********)
let tab3 = GPack.vbox ()
let name_tab3 = GMisc.label ~text:"Modélisation" ()
let addTab3 = tabControl#insert_page
	~tab_label:name_tab3#coerce tab3#coerce



(* toolbar > barre d'outils du haut *)
let toolbar = GButton.toolbar
	~orientation:`HORIZONTAL
	~style:`BOTH
	~packing:(hbox1#pack ~expand:true) ()



(********** toolbar buttons ********)
(* each button is in a separate vbox *)

(* vBox > button OuvrirUneImage and Précision *)
let vbox2 =
	let vbox = GPack.vbox
		~border_width:10
		~packing:toolbar#add ()
	in vbox


(* filter > filter for image formats *)
let imgFilter = GFile.filter
	~name:"Fichier image"
	~patterns:["*.bmp";"*.jpg";"*.jpeg";"*.png"]()


(* button > new project *)
let button2AA =
	let button = GButton.button
		~label:"Nouveau projet"
		~packing:vbox2#add ()
	in button


(* GMisc.label > display selected-image path *)
let button2AB =
	let label = GMisc.label
		~text:"Pas de fichier sélectionné"
		~packing:vbox2#add ()
	in label


(* GMisc.label > button "choisir une image" *)
let button2B =
	let label = GMisc.label
		~text:
			"> Cliquez ci-dessous pour commencer le processus de modélisation."
		~packing:tabAccueil#add ()
	in label


(* button > button to open an image *)
let button2A =
	let button = GFile.chooser_button
		~action:`OPEN
		~width:300
		~packing:tabAccueil#add ()
	in button





(* separator_tool_item > separator between vbox2 and vbox4 *)
let separator2_4 =
	let separator = GMisc.separator `VERTICAL
		~packing:toolbar#add ()
	in separator


(* vBox > radio_button to choose language *)
let vbox4 =
	let vbox = GPack.vbox
		~border_width:10
		~packing:toolbar#add ()
	in vbox


(* radio_button > english *)
let button4A =
	let button = GButton.radio_button
		~label:"anglais"
		~packing:vbox4#add ()
	in button


(* radio_button > french *)
let button4B =
	let button = GButton.radio_button
		~label:"francais"
		~group:button4A#group
		~packing:vbox4#add ()
	in button


(* separator_tool_item > separator between vbox4 and vbox6 *)
let separator4_6 =
	let separator = GMisc.separator `VERTICAL
		~packing:toolbar#add ()
	in separator


(* vBox > radio_button for "lissage" *)
let vbox6 =
	let vbox = GPack.vbox
		~border_width:10
		~packing:toolbar#add ()
	in vbox


(* radio_button > mode "doux" *)
let button6A =
	let button = GButton.radio_button
		~label:"Lissage doux"
		~packing:vbox6#add ()
	in button


(* radio_button > mode "accentué" *)
let button6B =
	let button = GButton.radio_button
		~label:"Lissage accentué"
		~packing:vbox6#add ()
		~group:button6A#group
	in button


(* separator_tool_item > separator between vbox 6 and vbox 8 *)
let separator6_8 =
	let separator = GMisc.separator `VERTICAL
		~packing:toolbar#add ()
	in separator


(* vBox > begin process and display current step *)
let vbox8 =
	let vbox = GPack.vbox
		~border_width:10
		~width:200
		~packing:toolbar#add ()
	in vbox


(* button > button for next step launching *)
let button8B =
	let label = GMisc.label
			~text:"En attente d'un projet"
			~packing:vbox8#add ()
		in label


let button8A =
	let button = GButton.button
		~label:!currentStepString
		~packing:vbox8#add ()
	in button


(* separator_tool_item > separator between vbox8 and vbox10 *)
let separator8_10 =
	let separator = GMisc.separator `VERTICAL
		~packing:toolbar#add ()
	in separator


(* vBox > begin process and display current step *)
let vbox10 =
	let vbox = GPack.vbox
		~border_width:10
		~packing:toolbar#add ()
	in vbox


(* button > about button for information of the program *)
let button10A =
	let bdd = GWindow.about_dialog
		~name:"Let's map the world !"
		~authors:["Sweepy"]
		~copyright:"Copyright 2011-2012 Let's map the world"
		~version:"Bêta 1.0"
		~website:"http://www.aventureweb.com/projetcarto/"
		~website_label:"LMTW online ?"
		~position:`CENTER_ON_PARENT
		~parent:window
		~destroy_with_parent:true ()
	in let button = GButton.button
		~stock:`ABOUT
		~packing:vbox10#add () in
		GMisc.image
			~stock:`ABOUT
			~packing:button#set_image ();
		button#connect#clicked (fun () ->
			ignore (bdd#run ()); bdd#misc#hide ());
		button


(* button > button to quit the program *)
let button10B =
	let button = GButton.button
		~label:"Quitter LMTW"
		~stock:`QUIT
		~packing:vbox10#add ()
	in button#connect#clicked ~callback:GMain.quit


(******* functions called by interface buttons ********)

(* fixAccuracy > change displayAccuracy value *)
let set_accuracy_label () =
	if (fixAccuracy#adjustment#value >= 1.) then
		begin
		let x = (fixAccuracy#adjustment#value) in
			begin
			if ((imageToParse#get_imageMax)/(int_of_float x) < 10) then
				displayAccuracy#set_text (
					"Maillage d'échantillonnage : 00"
					^ string_of_int(
						(imageToParse#get_imageMax)/(int_of_float x))
					^ " px."
				)
			else
				begin
				if ((imageToParse#get_imageMax)/(int_of_float x) >= 100) then
					displayAccuracy#set_text (
						"Maillage d'échantillonnage : "
						^ string_of_int(
							(imageToParse#get_imageMax)/(int_of_float x))
						^ " px."
						)
				else
					displayAccuracy#set_text (
						"Maillage d'échantillonnage : 0"
						^ string_of_int(
							(imageToParse#get_imageMax)/(int_of_float x))
						^ " px."
					)
				end
			end
		end


(* fixCorrection > change displayCorrectionArtefacts value *)
let set_correction_label () =
		let x = (fixCorrection#adjustment#value) in
			if (x < 10.) then
				displayCorrectionArtefacts#set_text
					("Correction des artefacts :\nnulle")
			else
				if (x < 30.) then
					displayCorrectionArtefacts#set_text
						("Correction des artefacts :\ndouce")
				else
					if (x > 30. && x < 70.) then
						displayCorrectionArtefacts#set_text
							("Correction des artefacts :\nmodérée")
					else
						displayCorrectionArtefacts#set_text
							("Correction des artefacts :\nforte")




let validateColor color altitude button () =
	button#set_label (altitude#text ^ "- sauvegardé...");
	let (r,g,b) = color in
		imageToParse#set_listColorsRGB (
			((r+g+b)/3, int_of_string altitude#text)
				::(imageToParse#get_listColorsRGB)
		)
(*print_endline (string_of_int (List.length imageToParse#get_listColorsRGB))*)




(* compare two colors of listColorRGB *)
let compareColors a b =
	let (c1,_) = a and (c2,_) = b in
	if (c1 > c2) then
		(1)
	else if (c1 == c2) then
		(0)
	else
		(-1)


(* parse image to get a .obj file *)
let createObj () =
	begin
	(*print_endline ("LONGUEUR : 
		" ^ string_of_int(List.length imageToParse#get_listColorsRGB));*)
	print_endline "will create *.obj";
	imageToParse#create_obj ()
	end



(* update min value *)
let set_min_value x () =
    imageToParse#set_minAltitude (x)



(* update max value *)
let set_max_value x () =
    imageToParse#set_maxAltitude (x)



let validateMin entry button () =
	button#set_label (entry#text ^ "- sauvegardé...");
	set_min_value (int_of_string entry#text);
	imageToParse#set_minAltitude (int_of_string entry#text)



let validateMax entry button () =
	button#set_label (entry#text ^ "- sauvegardé...");
	set_max_value (int_of_string entry#text);
	imageToParse#set_maxAltitude (int_of_string entry#text)



(* display found colors in the right frame *)
let rec displayColors l =
	imageToParse#empty_listColorsRGB;
	match l with
	| x::l -> let (r,g,b) = x#get_color in
		let box = GPack.hbox
			~packing:boxContentOptions#add ()
		in box;
		let entry = GEdit.entry
			~text:""
			~width:80
			~height:40
			~packing:box#add ()
		and  button = GButton.button
			~label:"> Valider"
			~packing:box#add ()
		in let color = Printf.sprintf "#%02x%02x%02x" r g b
		in begin
			entry#misc#modify_base [`NORMAL, `NAME color];
			button#connect#clicked ~callback:
				(validateColor (r,g,b) entry button);
			button#set_border_width 20;
		end;
		displayColors l
	| _ -> ()




let displayMinMax l =

		    let text = GMisc.label
			~text:"Lissage et interpolation des altitudes..."
			~packing:boxContentOptions#add ()
		    in text;


    		    let separator = GMisc.separator `HORIZONTAL
			~packing:boxContentOptions#add ()
		    in separator;


		    let box1 = GPack.hbox
			~packing:boxContentOptions#add ()
		    in box1;

		    let entryMin = GEdit.entry
			~width:80
			~height:40
			~packing:box1#add ()
		    and button1 = GButton.button
			~label:"> Valeur du blanc (255,255,255)"
			~packing:box1#add ()
		    in begin
			entryMin#misc#modify_base [`NORMAL, `WHITE];
			button1#connect#clicked ~callback:(validateMin entryMin button1)
		    end;


		    let box2 = GPack.hbox
			~packing:boxContentOptions#add ()
		    in box2;


		    let entryMax = GEdit.entry
			~text:""
			~width:80
			~height:40
			~packing:box2#add ()
		    and button2 = GButton.button
			~label:"> Valeur du noir (0,0,0)"
			~packing:box2#add ()
		    in begin
			button2#connect#clicked ~callback:(validateMax entryMax button2)
		    end;

		    let separator2 = GMisc.separator `HORIZONTAL
			~packing:boxContentOptions#add ()
		    in separator2;


		    let contentImg23 = GBin.frame
			~packing:tabAccueil#add ()
		    in contentImg23;


			let imgFrame = GMisc.image
			~file:"imgFiles/lmtw_little.jpg"
			~packing:boxContentOptions#add ()
			in imgFrame;

		    imgFrame_mapped#set_file (imageToParse#get_imagePath ^ "mapped.bmp")



let changeStepString () =
	if (!currentStepInt == 0) then (* initialisation > échantillonnage *)
		begin
		currentStepString := "Initialisation";
		currentStepInt := !currentStepInt + 1;
		(*progressBar#set_text "Carte chargee";*)
		(*progressBar#set_fraction 1.0;*)
		set_accuracy_label ();
		button8B#set_text "En cours : initialisation";
		end
	else if (!currentStepInt == 1) then (* échantillonnage > modélisation *)
		begin
		currentStepString := "> Echantillonnage";
		currentStepInt := !currentStepInt + 1;
		set_accuracy_label ();
		set_correction_label ();
		progressBar_bar#set_text "Image chargee";
		progressBar_bar#set_fraction (progressBar_bar#fraction +. (1.)
			/. (float_of_int numberTotalStep));
		button8B#set_text "En cours : paramétrage";
		end
	else if (!currentStepInt == 2) then (* itineraire > modélisation *)
		begin
		currentStepString := "> Itinéraire";
		currentStepInt := !currentStepInt + 1;
		progressBar_bar#set_text "Paramétrage de l'échantillonnage...";
		progressBar_bar#set_fraction (progressBar_bar#fraction +. (1.)
			/. (float_of_int numberTotalStep));
		imageToParse#set_imageMaillageSize (imageToParse#get_imageMax
			/ int_of_float (fixAccuracy#adjustment#value));
		(*imageToParse#set_imageMaillageSize 2;*)
		imageToParse#set_imageCorrectionArtefacts
			(int_of_float(fixCorrection#adjustment#value));
		imageToParse#set_imageWantToChangeColors (wantToSaveColors#active);
		imageToParse#set_imageWantToAddObliques (wantTheObliques#active);
		imageToParse#set_traitementColor (heightMapFalse#active);
		if (buttonWhichTrace_noir#active == true) then
			imageToParse#set_imageOpacityLines (100)
		else
			if (buttonWhichTrace_opacityMedium#active == true) then
				imageToParse#set_imageOpacityLines (60)
			else
				imageToParse#set_imageOpacityLines (20);
		if (heightMapFalse#active) then (* mode without lissage colorized *)
		begin
			imageToParse#transformPlanMap;
			imgFrame_mapped#set_file(imageToParse#get_imagePath ^ "mapped.bmp");
			(* insertion des couleurs *)
			let text = GMisc.label
				~text:"Saisissez les altitudes respectives pour chaque couleur."
				~packing:boxContentOptions#add ()
			in text;
			displayColors (imageToParse#get_listDisplayColors);
		end
		else (* mode with lissage, grey scale *)
		begin
		imageToParse#transformHeightMap;
		displayMinMax ();
		end;
		(*print_endline ("youhouuuu : "
			^ string_of_int(List.length imageToParse#get_listDisplayColors));*)
		button8B#set_text "En cours : échantillonnage"
		end
	else if (!currentStepInt == 3) then
		begin
		button8B#set_text "En cours : itinéraire";
		currentStepInt := !currentStepInt + 1;
		currentStepString := "> Modélisation";
		imageToParse#set_departureIti !iti_depart;
		imageToParse#set_arrivalIti !iti_arrivee;
		progressBar_bar#set_text "Calcul de votre itineraire...";
		progressBar_bar#set_fraction
			(progressBar_bar#fraction +. (1.) /.(float_of_int numberTotalStep));
		imgFrame_algo#set_file imageToParse#get_imagePath;
		iti_arrivee :=
			(imageToParse#get_imageWidth, imageToParse#get_imageHeight);
		end
	else if (!currentStepInt == 4) then
		begin
		button8B#set_text "En cours : modélisation";
		currentStepInt := !currentStepInt + 1;
		currentStepString := "> Navigation";
		progressBar_bar#set_text "Navigation...";
		progressBar_bar#set_fraction 
			(progressBar_bar#fraction +. (1.) /.(float_of_int numberTotalStep));
		if (heightMapFalse#active) then
			createObj ()
		else
			imageToParse#createHeightObj ()
		(*print_endline "ok"*)
		end
	else
		(*print_endline "wait"*) ()


(* go to next step *)
let change_step () =
	if (!currentStepInt > 0) then
		begin
		changeStepString ();
		tabControl#next_page ();
		button8A#set_label !currentStepString
		end
	else ()


(* button2A > change tab"2D" image *)
(* when user chooses another one *)
let start_process () =
	
	Gaux.may imgFrame#set_file button2A#filename;
	button2AB#set_text "Projet en cours...";
	currentStepInt := !currentStepInt + 1;
	let fileName = match (button2A#filename) with
		| Some x -> x
		| _ -> "error"
		in
		begin
			imageToParse#set_imagePath fileName;
			imageToParse#set_imageSize;
			change_step ();
		end


(* button2AA > called when user wants to start a new project *)
let empty_for_new_project () =
	currentStepInt := 0;
	currentStepString := "Initialisation";
	progressBar_bar#set_text "En attente d'un projet";
	progressBar_bar#set_fraction 0.


(* manage keyboard and mouse events *)
let rec wait_event () =
	let e = Sdlevent.wait_event () in
	match e with
		| Sdlevent.KEYDOWN _ -> ()
		| _ -> wait_event ()


(* set the departure point when mouse is pressed *)
let set_departure_point x y =
	if (x < imageToParse#get_imageWidth &&
	y < imageToParse#get_imageHeight) then
		imageToParse#set_departureIti (x,y);
		imageToParse#draw_iti;
		imgFrame_algo#set_file (imageToParse#get_imagePath ^ "itineraire.bmp");
	true


(* set the departure point when mouse is pressed *)
let set_arrival_point x y =
	if (x < imageToParse#get_imageWidth &&
	y < imageToParse#get_imageHeight) then
		imageToParse#set_arrivalIti (x,y);
		imageToParse#draw_iti;
		imgFrame_algo#set_file (imageToParse#get_imagePath ^ "itineraire.bmp");
	true


let trace_iti () =
	imageToParse#process_path;
	imgFrame_algo#set_file (imageToParse#get_imagePath ^ "itineraireDone.bmp");
	labelDenivelate#set_text "lolilol"


(* main *)
let main_interface () =
	window#show ();
	button2AA#set_border_width 10;
	button2AA#connect#clicked ~callback:(empty_for_new_project);
	buttonTest#connect#clicked ~callback:(trace_iti);
	imgFrame_algo#set_file imageToParse#get_imagePath;
	eventBox_algo#event#connect#button_press ~callback:(fun t ->
		let x = truncate (GdkEvent.Button.x t)
		and y = truncate (GdkEvent.Button.y t) in
		if ((GdkEvent.Button.button t) == 1) then
			set_departure_point x y
		else
			set_arrival_point x y);
	progressBar_bar#set_text "En attente d'un projet";
	progressBar_bar#set_fraction 0.;
	button2A#set_border_width 50;
	boxContentOptions#set_border_width 10;
	(* change step except at the beginning *)
	button8A#connect#clicked ~callback:(change_step);
	(* refresh frame image after being changed *)
	button2A#connect#selection_changed ~callback:(start_process);
	fixAccuracy#connect#value_changed ~callback:(set_accuracy_label);
	fixCorrection#connect#value_changed ~callback:(set_correction_label)
